<?php
$title = "OnlineShop - Search";
$css = "search.css";
include_once 'common/top.php';
//include_once '../Class/DB.php';
include_once 'load_class.php';//include DB.php
//$db=new DB();
$page=$_GET["page"];
if(!isset($page))
{
	$page=1;
}
$numPerPage=10;
$meta=$_REQUEST["key"];
$price_check=$_REQUEST["price"]==""?false:$_REQUEST["price"];
$b_tid=$db->get_name_tid($_REQUEST["classify1"],"b");
$s_tid=$db->get_name_tid($_REQUEST["classify2"],"s");
$low_price=$_REQUEST["price1"];
$high_price=$_REQUEST["price2"];
$b_name=$_REQUEST["classify1"];
$s_name=$_REQUEST["classify2"];
if(isset($_REQUEST["key"])){//searching...
	if($meta!="")
	{
	$search_results=$db->get_meta_product($meta);
	$search_results=$db->search_sort($search_results,$b_tid,$s_tid,$low_price,$high_price);
	}
	else 
	{
		
	$search_results=$db->get_all_product();
	$search_results=$db->search_sort($search_results,$b_tid,$s_tid,$low_price,$high_price);	
	//echo count($search_results);
	//exit();
	}
	$totalPage=ceil(count($search_results)/$numPerPage);
	$startCount=($page-1)*$numPerPage;
	$search_results=$db->search_paging($search_results,$startCount,$numPerPage);
}
else 
{
unset($search_results[0]);
}

?>
<script src="../js/search.js" type="text/javascript"></script>
<div id="search">
	<form action="search.php" method="post">
		<div id="search_bar">
			<div id="search_bar_ls"></div>
			<div id="search_bar_ms"><input type="text" name="key" value="<?php echo $meta;?>"/></div>
			<input type="submit"" value=""/>
		</div>
	<div id="search_classify">
		<p>Category</p>
		<select name="classify1" onchange="on_select(this);" id="big">
			<option value="0">All categories</option>
			<?php 
			$c_name=$db->get_parent_types(0);
			$i=0;
			while(isset($c_name[$i])){
	       		//$c_name=$db->get_tid_types($i+1);
	       	?>
	        <option value="<?php echo $c_name[$i]["name"];?>"><?php echo $c_name[$i]["name"];?></option>
			<?php 
			$i++;	
			}
			?>
		</select>
		<select name="classify2" id="small">
			<option value="0">All categories</option>
		</select>
	</div>
	<div id="search_price">
		<input type="checkbox" name="price" onclick="priceCheck(this);" <?php if($price_check){echo 'checked="true"';}?>/>
		<label>Price:</label>
		<input type="text" id="price1" <?php if(!$price_check){echo 'disabled="disabled"';} ?> name="price1" value="<?php echo $low_price;?>"/>
		<label>-</label>
		<input type="text" id="price2" <?php if(!$price_check){echo 'disabled="disabled"';} ?> name="price2" value="<?php echo $high_price;?>"/>
	</div>
	</form>
</div>
<?php if(isset($_REQUEST["key"])){?>
<div id="result" class="block_mode">
	<h3>Results</h3>
	<div class="block_context">
		<?php 
		$i=0;
		while(isset($search_results[$i])){
		?>		
		<div class="adv">
			<a href="item.php?id=<?php echo $search_results[$i]["pid"];?>"><img alt="good" src="<?php if(file_exists("../Image/product_img/".$search_results[$i]["pid"].".jpg")){echo "../Image/product_img/".$search_results[$i]["pid"].".jpg";}else{echo "../Image/product_img/item_none.png";}?>"></a>
			<h4><a href="item.php?id=0"><?php  echo $search_results[$i]["p_name"];?></a></h4>
			<p class="no">$<?php echo $search_results[$i]["ori_price"];?></p>
			<p>$<?php echo $search_results[$i]["act_price"];?></p>
		</div>
		<?php 
		$i++;
		}
		if($i==0&&isset($_REQUEST["key"]))
		echo "Sorry, can not find anything you want.";
		?>
		
		<div class="clearfix"></div>
		<?php 
		if($i!=0){
		?>
	      <div id="page">
			<?php if ($page != 1) { ?>
			<a href="search.php?&classify1=<?php echo $b_name;?>&classify2=<?php echo $s_name?>&price1=<?php echo $low_price;?>&price2=<?php echo $high_price;?>&key=<?php echo $meta;?>&price=<?php echo $price_check;?>&page=<?php echo $page - 1;?>">Back</a> 
			<?php 
			}
		    for ($i=1;$i<=$totalPage;$i++) {  
				if($i==$page){
					?>
					<p style="color:black;display: inline-block;"><?php echo $i;?></p>
			<?php continue;}
			?>
			<a href="search.php?&classify1=<?php echo $b_name;?>&classify2=<?php echo $s_name?>&price1=<?php echo $low_price;?>&price2=<?php echo $high_price;?>&key=<?php echo $meta;?>&price=<?php echo $price_check;?>&page=<?php echo $i;?>"><?php echo $i;?></a> 
			<?php
			}
			if ($page<$totalPage) { 
			?>
				<a href="search.php?&classify1=<?php echo $b_name;?>&classify2=<?php echo $s_name?>&price1=<?php echo $low_price;?>&price2=<?php echo $high_price;?>&key=<?php echo $meta;?>&price=<?php echo $price_check;?>&page=<?php echo $page + 1;?>">Next</a> 
			<?php
					} 
			?>
			
			</div>
		<?php 
		}
		?>
	</div>
</div>
<?php }?>
<script language="JavaScript">
var bb='<?php print_r($b_name)?>';
var ss='<?php print_r($s_name)?>';
on_init(bb,ss);
</script>
<?php
include_once 'common/tail.php';
?>